package com.ltx.blog_ltx.server;

import com.ltx.blog_ltx.entity.Answer;
import com.ltx.blog_ltx.until.CommandUtil;

import java.io.FileWriter;
import java.io.IOException;

public class OJ_CServer {
    static String DIR=System.getProperty("user.dir");
    static String CFile=DIR+"/Main.c";
    static String CEXE="Main";
    static String compileCmd="gcc Main.c -o Main";
    static String runningCmd="./Main";

    public static Answer returnAnswer(String doc) throws IOException {
        FileWriter writer=new FileWriter(CFile);
        writer.write(doc);
        writer.close();
        //编译源文件为class文件
        Answer answer= CommandUtil.run(compileCmd,false,true);    //编译文件不需要获得标准报错
        //若编译成功即可开始运行
        if(answer.getError()==0) {
            answer = CommandUtil.run(runningCmd, true, true);    //运行文件需要输出和错误的输出信息所以两个都要是ture
            if(answer.getError()==0)
                answer.setReason(Answer.Success);
            else
                answer.setReason(Answer.RuntimeError);
        }
        else
            answer.setReason(Answer.Error);
        return answer;
    };

}
